package com.heima.media.task;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ArticleTask {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private IWmNewsService wmNewsService;

    @Autowired
    private IAuditService auditService;

    @Async
    public void handleMessage() throws Exception{

        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("news-dqueue");

        //获取延迟队列
        while (true){
            Object result = blockingDeque.poll(30, TimeUnit.SECONDS);
            if (result!=null){
                //result是wmNews的id
                WmNews wmNews = wmNewsService.getById(Integer.parseInt(result.toString()));

                Long articleId = auditService.saveApArticle(wmNews);
                wmNews.setArticleId(articleId);

                wmNews.setStatus(9);
                wmNewsService.updateById(wmNews);

                log.info("延时任务发布成功");
            }
        }

    }
}
